<?php
/**
 * 图书数据模型
 * 
 * @author sandy
 *
 */
class bookModel extends Model {
	
	/**
	 * 获取指定分页图书信息
	 * 
	 * @param array $page_info 分页信息
	 */
	public function get_all_book($page_info = array()){
		
		$return = array('result'=>'','total'=>0);
		
		$sql = "SELECT b.*,a.author_name,s.structure_name 
				FROM #__books b 
				LEFT JOIN #__author a 
				ON b.author_id = a.author_id 
				LEFT JOIN #__structure s 
				ON b.structure_id = s.structure_id 
				ORDER BY b.book_id DESC";
		
		$sql_count = "	SELECT count(*) 
						FROM #__books b 
						LEFT JOIN #__author a 
						ON b.author_id = a.author_id 
						LEFT JOIN #__structure s 
						ON b.structure_id = s.structure_id";
		
		if (!empty($page_info)){
			$this->db->set_query($sql,$page_info['offset'],$page_info['limit']);
		} else {
			$this->db->set_query($sql);
		}
		
		$return['result'] = $this->db->get_all();
		
		$this->db->set_query($sql_count);
		$return['total'] = $this->db->get_one();
		
		return $return;
	}
	
	/**
	 * 获取指定图书信息
	 * @param int $book_id
	 */
	public function get_book_info($book_id){
		
		$book_id = intval($book_id);
		if (!$book_id){
			return false;
		}
		
		$sql = "SELECT b.*,a.author_name,s.structure_name 
				FROM #__books b 
				LEFT JOIN #__author a 
				ON b.author_id = a.author_id 
				LEFT JOIN #__structure s 
				ON b.structure_id = s.structure_id 
				WHERE b.book_id = $book_id";
		
		$this->db->set_query($sql);
		
		return $this->db->get_row();
	}

	public function add_book($book_info){
		$insert_info = array(
			'book_name' => $book_info['book_name'],
			'author_id' => $book_info['author_id'],
			'category_id' => $book_info['category_id'],
			'structure_id' => $book_info['structure_id'],
			'abstract' => $book_info['abstract'],
			//'status' => 1,
			'addtime' => time(),
			//'publictime' => '',
			//'cover_url' => ''
				
		);
		
		return $this->db->insert("#__books",$insert_info);
	}
	
	public function update_book($book_info){
		
		$update_info = array(
				'book_name' => $book_info['book_name'],
				'author_id' => $book_info['author_id'],
				'category_id' => $book_info['category_id'],
				'structure_id' => $book_info['structure_id'],
				'abstract' => $book_info['abstract'],
				//'status' => 1,
				//'publictime' => '',
				//'cover_url' => ''
		);
		
		return $this->db->update('#__books',$update_info,'book_id = '.$book_info['book_id']);
	}

}


